﻿'use strict';

// todoPage controller
app.controller('goodsType', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        title:'货品类别列表',
        addTitle:'货品类别信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };
    $scope.setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeClick: showDetail
        }
    };
    $scope.selectNode = {};
    function showDetail(treeId, treeNode, clickFlag){
        //此处进行列表筛选，显示子节点列表
        $scope.saveDate = [];
        $scope.saveDateAll.forEach(function(item){
            if(treeNode.id==1){
                $scope.saveDate.push(item);
            }else{
                if(treeNode.id==item.topId){
                    $scope.saveDate.push(item);
                }
            }
        });
        $scope.selectNode = treeNode;
        $scope.$apply();
        setTimeout(function(){
            $('.table').trigger('footable_redraw');
        }, 100);
    }
    function getGoodsTypeList(){
        $scope.doing_async = true;
        toaster.getInfo('/a_yongan/selectlist/goodtype',null).then(function(data){
            if(data && data.data && data.data.data){
                madeTree(data.data.data);
            }
        });
    }
    function madeTree(data){
        data.forEach(function(item){
           if(item.id==="1"){
               $scope.selectNode = item;
           }
        });
        setTimeout(function() {
            $.fn.zTree.init($("#mytree"), $scope.setting, data);
        },100);
        $scope.doing_async = false;
    }
    getGoodsTypeList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
        $scope.saveDateAll = data;
    }
    function addDo(){
        var notesdata = {};
        if(!$scope.goodsTypeRow.goodtypeId){
            notesdata = {//新增
                "goodtypeId":"",
                "qy":$("#qy").prop("checked"),
                "topId":$("#topId").val(),
                "topName":$("#topName").val(),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "goodtypeNumber":$("#goodtypeNumber").val(),
                "goodtypeMc":$("#goodtypeMc").val(),
                "goodtypeBz":$("#goodtypeBz").val()
            }
        }else{
            notesdata = {
                "goodtypeId":$scope.goodsTypeRow.goodtypeId,
                "qy":$("#qy").prop("checked"),
                "topId":$("#topId").val(),
                "topName":$("#topName").val(),
                "goodtypeNumber":$("#goodtypeNumber").val(),
                "goodtypeMc":$("#goodtypeMc").val(),
                "goodtypeBz":$("#goodtypeBz").val()
            }
        }
        var info="";
        toaster.postInfo('/a_yongan/add/goodtype',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.warehouseAdd = !($scope.warehouseAdd);
                $scope.warehouseList = !($scope.warehouseList);
                getGoodsTypeList();
                getList();
                return info;
            }else{
                showMessage('warning','',"保存失败！");
                info = "请求错误！";
            }
        });
        return info;
    }
    function getList(){
        toaster.getInfo('/a_yongan/list/goodtype',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
            }
        });
    }
    getList();
    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['goodtypeMc','goodtypeNumber'];
        var itemsValue = ['类别名称','类别编码'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.showSelectGoodsType = function(name,id){
        var modalInstance = $modal.open({
            templateUrl: 'selectGoods.html',
            controller: 'selectGoodsTypeCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $("#"+name).val(selectedItem.name);
            $("#"+id).val(selectedItem.id);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.message = '是否确定保存？';
    $scope.saveGoodsType = function (size) {
        if($scope.goodsTypeRow.goodtypeId===$("#topId").val()){
            showMessage('warning','',"此类品不能是自己的父节点！");
            return;
        }
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            var info = addDo();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(path) {
        $scope.goodsTypeRow = {};
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        getGoodsTypeList();
        $scope.goodsTypeRow = {
            'topName':$scope.selectNode.name,
            'topId':$scope.selectNode.id,
            'createTime':$filter("date")(new Date(), "yyyy/MM/dd"),
            'zdr':$scope.app.userName,
            'qy':true,
            'goodtypeNumber':'HPLB'+$filter("date")(new Date(), "yyyyMMddHHmmss")
        };
    };
    $scope.goodsTypeRow = {};
    $scope.columns = [
        {
            title:'类别编码',
            show:true
        },
        {
            title:'类别名称',
            show:true
        },
        {
            title:'创建时间',
            show:true
        },
        {
            title:'类别状态',
            show:true
        }
    ];
    $scope.showSelectDept = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectDept.html',
            controller: 'selectDeptCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $('#'+id+'').val(selectedItem.name);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.saveDate=[];
    /*
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:true
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'禁用中...',
            status1:'label bg-danger',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀体',
            lbbm:'KH20181112006',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            lbmc:'阀座',
            lbbm:'KH20181112005',
            bz:'无备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        }

     */
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        $scope.goodsTypeRow = row;
        $('#qy').prop("checked",$scope.goodsTypeRow.qy);
    }
}])
;